package com.zztpc.easybuy.db;


import com.zztpc.easybuy.util.MyUtil;

import java.sql.Connection;


import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Map;


public class MysqlDB implements IDB{
    private Connection conn;
    private Map map;
    /*private String DRICER="com.mysql.jdbc.Driver";
    private String URL="jdbc:mysql://localhost:3306/easybuy?useUnicode=true&characterEncoding=utf-8";
    private String UNAME="root";
    private String PWD="root";
    */
    private String DRICER;
    private String URL;
    private String UNAME;
    private String PWD;
    @Override
    public Connection getConnection() throws SQLException {
        try {
            map=MyUtil.readXML();//用自己写的工具类读取配置文件信息
            DRICER=(String) map.get("driver");
            URL=(String) map.get("url");
            UNAME=(String) map.get("uname");
            PWD=(String) map.get("pwd");
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            Class.forName(DRICER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        conn=DriverManager.getConnection(URL,UNAME,PWD);
        return conn;
    }

    @Override
    public void closeConn() throws SQLException {
        if (conn != null&&!conn.isClosed()) {
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        MysqlDB m=new MysqlDB();

        try{
            System.out.println(m.getConnection().isClosed());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
